package com.example.intelligentattendancesystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.intelligentattendancesystem.pojo.Absence;
import com.example.intelligentattendancesystem.pojo.History;
import com.example.intelligentattendancesystem.pojo.User;
import com.example.intelligentattendancesystem.pojo.Work;
import com.example.intelligentattendancesystem.service.LateService;
import com.example.intelligentattendancesystem.service.SignService;
import com.example.intelligentattendancesystem.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static java.lang.Integer.parseInt;

/**
 *
 * @author
 * @version 1.0
 * @className SignService
 * @since 1.0
 */
@RestController
@RequestMapping("smartAttendance/sign")
public class SignController {

    @Autowired
    SignService signService;
    @Autowired
    Work work;
    @Autowired
    LateService lateService;
    @Autowired
    UserService userService;
    /**
     * 获取当前班次没有打卡的员工，就是当前班次开始时间提前十分钟到当前时间没有打卡记录的员工
     * @param workTime     当前班次开始前十分钟的时间
     * @return  获取没有打卡的员工
     */
    @RequestMapping("getNoSignUsers")
    public List<User> getNoSignUser(String workTime){
        List<User> users = signService.getNoSignUserService(workTime);
        return users;
    }

    /**
     * 获取当前班次成功打卡的员工
     * @param workTime
     * @return
     */
    @RequestMapping("getSucAttUser")
    public List<History> getSucAttUser(String workTime){
        return signService.getSucAttUser(workTime);
    }
    /**
     * 获取当前班次需要打卡的员工
     * @param workTime
     * @return
     */
    @RequestMapping("getNeedActUsers")
    public List<User> getNeedActUsers(String workTime){
        List<Integer> userIds = signService.getNeedActUsers(workTime);
        return userService.getUsersService(userIds);
    }


    /**
     * 获取当前班次迟到的员工
     * @param week  当前星期
     * @param currentTime 当前时间
     * @param startTime     该时刻表格开始时间
     * @param preEndTime    前一时刻表格结束的时间
     * @return  获取打卡迟到的员工
     */
    @RequestMapping("getLateUser")
    public List<User> getLateUser(String currentTime ,String week,String startTime,String preEndTime){
        List<User> lateUserService = signService.getLateUserService(currentTime,week,startTime,preEndTime);
        return lateUserService;
    }

    /**
     * 打卡记录的增加（考勤），还需要判断迟到的学生，迟到就添加到迟到表
     *   userId    用户id
     *   position  打卡地点
     *   currentTime   当前时间
     *   endTime       当前班次结束时间
     *   startTime     当前班次开始时间
     *   week
     * @return
     */
    @PostMapping ("clockIn")
    public boolean clockIn(HttpServletRequest req){
        int userId = 0;
        String position = null;
        String currentTime = null;
        String endTime = null;
        String startTime = null;
        String week = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
            StringBuffer sb=new StringBuffer();
            String s=null;
            while((s=br.readLine())!=null){
                sb.append(s);
            }
            JSONObject jsonObject = JSONObject.parseObject(sb.toString());
            String suserId = jsonObject.getString("userId");
            userId = parseInt(suserId);
            position = jsonObject.getString("position");
            currentTime = jsonObject.getString("currentTime");
            endTime = jsonObject.getString("endTime");
            startTime = jsonObject.getString("startTime");
            week = jsonObject.getString("week");
        } catch (IOException e) {
            e.printStackTrace();
        }

        boolean b = signService.clockInService(userId, position, currentTime, endTime, startTime, week);
        return b;
    }

    /**
     * 本月的打卡记录
     * @param currentTime
     * @param userId
     * @return
     */
    @RequestMapping("signRecord")
    public List<History> signRecord(String currentTime , int userId){
        List<History> record = signService.getSignRecord(currentTime,userId);
        return record;
    }

}
